<?php 
if (!defined("MCTDESIGN")) die("# ");

switch($act){
	case "searchTour":
	   $title = "Tìm kiếm Tour";
	   $content = show_search_tour();
       break;  
   default : 
	   $content = header("Location: home.html");
	   break;     
}
function RootTourName($id){
    if($id == 1){
        return "tour-nuoc-ngoai";
    }else{
        return "tour-trong-nuoc";
    }
}
function show_search_tour(){
    global $tb_fix,$temp,$anyDB,$dir_tpl;
    $temp->assign( 'base_tpl', "".$dir_tpl."" );
    $rootID = (int)$_GET['rootId'];
    $cat1 = (int)$_GET['cat1'];
    $cat2 = (int)$_GET['cat2'];    
    
    $pagegroup_size = 10;
    $page=_POST('p');
    if (!$page||$page==0){$page=1;}
    $limit=_POST('limit');
    if($limit==''||$limit>10) {$limit=10;}
    $offset=($page-1)*$limit;
    $li="limit $offset,$limit";
    
    $where = "t.tou_root_cat_id = $rootID ";
    if($cat1 >0){
        $where.= "and t.tou_category = $cat1";
    }
    if($cat2 >0){
        $where.= "and t.tkh_id = $cat2";
    }
    $sql = "select t.tou_id from ".$tb_fix."_tour as t
                    left join ".$tb_fix."_tour_menu as tm
                        on t.tou_category = tm.tme_id
                    left join ".$tb_fix."_tour_khoihanh as tk
                        on tk.tkh_id = t.tkh_id    
                    where 
                        $where";
                        //echo $sql;die();
    $q = $anyDB->query($sql);
    $n = $anyDB->numRows($q);
    $totalRecord = $n; // tong so ban ghi
    $sql = "select t.tou_id, t.tou_title,t.tou_code, t.tou_category, t.tour_cat_parent_id,t.tou_root_cat_id,
                t.tou_phuongtien, t.tkh_id, t.tou_desc, t.tou_price, t.tou_begin_time,tm.tme_title,tk.tkh_name 
                from ".$tb_fix."_tour as t
                    left join ".$tb_fix."_tour_menu as tm
                        on t.tou_category = tm.tme_id
                     left join ".$tb_fix."_tour_khoihanh as tk
                        on tk.tkh_id = t.tkh_id   
                    where 
                        $where
                     order by 
                        t.tou_id desc $li";                
    $q = $anyDB->query($sql);
    $rows = $anyDB->getAll($q);
    foreach($rows as $k => $v){
        $rows[$k]['tou_title'] = subCutString($v['tou_title'],17);
        $rows[$k]['tou_title_url'] = convertText($v['tou_title']);
        $rows[$k]['tou_cat_url'] = convertText($v['tme_title']);
        $rows[$k]['TourRootName'] = RootTourName($v['tou_root_cat_id']);
        $tourid = $v['tou_id'];
        $sql = "select mti_url from ".$tb_fix."_tour_img where tou_id = $tourid order by RAND() limit 0,1";
        $q = $anyDB->query($sql);
        if($anyDB->numRows($q)>0){
            $rImg = $anyDB->getAll($q);
            foreach($rImg as $key=>$val){
                $rows[$k]['images'] = $val['mti_url'];    
            }
            
        };
    }
    $temp->assign("rows",$rows);
    //BREAKCUM   
    if($rootID == 2){
        $txtSearched = "Đang tìm kiếm: Tour Trong Nước";
    }else{
        $txtSearched = "Đang tìm kiếm: Tour Nước Ngoài";
    }
    $temp->assign("RootCate",$txtSearched);    
    $catNameURL = convertText($rCatName['tme_title']);
    $getURL = getURLPaging();
    $pages = pagenavigator($page,$totalRecord,$limit,$pagegroup_size,"$getURL");
    $temp->assign('pages',$pages); 
    $content = $temp->fetch( 'modules/_timkiemtour.mct');
    return $content;
}
function getURLPaging(){
    $getURL = $_SERVER['REQUEST_URI'];
    $p = $_GET['p'];
    $newURL = str_replace("&p=$p","",$getURL);
    return $newURL;
}

/**
 * @author pavennt 
 * Description : load ajax by tour trong nước 
 *  
 */
 function tourtrongnuoc(){
    global $tb_fix,$temp,$anyDB,$dir_tpl;
    $temp->assign( 'base_tpl', "".$dir_tpl."" );
    $id = $_GET['id'];
    $ngay = $_GET['ngaykhoihanh'];
    if($ngay == 0){
        $where = '';
    }else{
        $where = " and t.tou_begin_time =".convertDateToData($ngay);    
    }
    
    $sql = "select * from ".$tb_fix."_tour as t left join ".$tb_fix."_tour_menu as c on t.tou_category = c.tme_id  where t.tou_category = $id  $where ORDER BY tou_id DESC";
    $query = $anyDB->query($sql);
    $total = $anyDB->numRows($query); 
    $arr  = $anyDB->getAll($query);    
    foreach($arr as $key => $value){
        $arr[$key]['thoigian'] = date('d-m-Y',$value['tou_begin_time']);
        $arr[$key]['tentuor'] = convertText($value['tou_title']);
        $arr[$key]['tenchatour'] = convertText($value['tme_title']);
        $arr[$key]['cat_mota']  = subCutString($value['tou_des'],20);
        $arr[$key]['gia']  = $value['tou_price'];
 
    }
    //echo "<pre>"; print_r($arr); exit();
    $temp->assign('datatour',$arr);
    $temp->assign('tikiemtour','Tour trong nước');
    $temp->assign('ngaykh',$ngay);
    $temp->assign('idtour',$id);
    $temp->assign('tongketqua',$total);
    return $temp->fetch( 'modules/_timkiemtour.mct' );

    
 }
 
function convertDateToData($date){
    $date = explode("-",$date);
    $nDate = $date[0]."-".$date[1]."-".$date[2];
    $sDate = strtotime($nDate);
    return $sDate;
}

/**
 *  
 * @author Nguyên tiến Pa Ven 
 * Description : tim kiem tour nuoc ngoài
 * 
 */
function tournuocngoai(){
    global $tb_fix,$temp,$anyDB,$dir_tpl;
    $temp->assign( 'base_tpl', "".$dir_tpl."" );
    $id = $_GET['id'];
    $ngay = $_GET['ngaykhoihanh'];
    if($ngay == 0){
        $where = '';
    }else{
        $where = " and t.tou_begin_time =".convertDateToData($ngay);    
    }
    
    $sql = "select * from ".$tb_fix."_tour as t left join ".$tb_fix."_tour_menu as c on t.tou_category = c.tme_id  where t.tou_category = $id  $where ORDER BY tou_id DESC";
    
    $query = $anyDB->query($sql);
    $total = $anyDB->numRows($query); 
    $arr  = $anyDB->getAll($query);    
    foreach($arr as $key => $value){
        $arr[$key]['thoigian'] = date('d-m-Y',$value['tou_begin_time']);
        $arr[$key]['tentuor'] = convertText($value['tou_title']);
        $arr[$key]['tenchatour'] = convertText($value['tme_title']);
        $arr[$key]['cat_mota']  = subCutString($value['tou_des'],20);
        $arr[$key]['gia']  = $value['tou_price'];
 
    }
    //echo "<pre>"; print_r($arr); exit();
    //echo $total;
    $temp->assign('datatour',$arr);
    $temp->assign('ngaykhnucngoai',$ngay);
    $temp->assign('tikiemtour','Tour nước ngoài');
    $temp->assign('idtournuocngoai',$id);
    $temp->assign('tongketqua',$total);
    //$temp->assign('',)
    return $temp->fetch( 'modules/_timkiemtour.mct' );    
}
/**
 *@author : pavennt
 *Description : Tim kiem khach san 
 * 
 * 
 */
 function khachsanvietnam(){
    global $tb_fix,$temp,$anyDB,$dir_tpl;
    $temp->assign( 'base_tpl', "".$dir_tpl."" );
    $id = $_GET['id'];
    $sql = "select h.*,c.* from ".$tb_fix."_hotels as h left join ".$tb_fix."_hotel_categorys as c on h.hos_hca_id 	= c.hca_id where h.hos_hca_id = $id ";
    $q = $anyDB->query($sql);
    $total = $anyDB->numRows($q);
    $arr = $anyDB->getAll($q);
    //echo "<pre>"; print_r($arr);
    foreach($arr as $key=>$value){
        $arr[$key]['tenkhaks'] = convertText($value['hos_title']);
        $arr[$key]['tennhomks'] = convertText($value['hca_title']);
        $arr[$key]['ks_mota']  = subCutString($value['hos_desc'],20);
        $temp->assign('ten',$value['hca_title']);       
    }
    $temp->assign('ketquatim',$arr);
    $temp->assign('tongketqua',$total);
    $temp->assign('ks',1);
    $temp->assign('idks',$id);
    return $temp->fetch( 'modules/_timkiemks.mct'); 
    
 }
/**
 * 
 * @author pavennt 
 * get ten khach san
 * 
 * 
 */ 
 function gettenkhachsan(){
      global $tb_fix,$temp,$anyDB,$dir_tpl;
      $id = $_GET['id'];
      $sql = "select * from ".$tb_fix."_hotels where hos_id = $id" ;
      //4echo $sql ; die();
      $q = $anyDB->query($sql);
      $arr = $anyDB->getNext($q);
      $html = "<input type='hidden' value='".$id."'  id='idks' /> ".$arr['hos_title']." (Tiêu Chuẩn : ".$arr['hos_star']." Sao) ";
      echo $html;
      exit();
 }
 
 /**
  * 
  *@author pavennt 
  * 
  * Des : get db kh anh send mail dăt phong
  * 
  */
  function  datphongpro(){
    global $tb_fix,$temp,$anyDB,$dir_tpl;
    $id = $_GET['id'];
    $name = $_GET['name'];
    $email = $_GET['email'];
    $phone = $_GET['phone'];
    $ngaykh = $_GET['ngaykh'];
    $diachi = $_GET['diachi'];
    $quoctich = $_GET['quoctich'];
    $nguoilon = $_GET['nguoilon'];
    $treem = $_GET['treem'];
    $yeucauthem = $_GET['yeucauthem'];
    $sql = "select h.*,c.* from ".$tb_fix."_hotels as h left join ".$tb_fix."_hotel_categorys as c on h.hos_hca_id 	= c.hca_id where h.hos_id = $id ";
    $q = $anyDB->query($sql);
    $arr = $anyDB->getNext($q);
    $link = "".get_data('shop_url','config_values')."KhachSan/".$arr['hos_hca_root_id']."/".convertText($arr['hos_title'])."/".$arr['hos_hca_id']."/".$arr['hos_id']."/".convertText($arr['hos_title'])."html";
   //echo $link; die();
	$subject = "Thông tin đặt Khách Sạn";		
    $mess = "";
    $mess .= "<div style=\"width: 100%;height: 25px;background: green; text-align: center;color: #ffffff;\"> <span style='display: block;font-weight: bold;padding-top: 1px !important;' ><strong>Thông tin đặt Khách Sạn  </strong></span></div>\n"; 
    $mess .= "<ul style=\"padding-left: 25px;position: relative;\">\n"; 
    $mess .= "    <li style=\"padding-top: 3px;\">Xin chào adminstrator: <strong> Có 1 khách hàng Đặt Khách Sạn tại web site http://postumtravel.vn thời gian gửi thông tin là ".date('d/m/Y - H:i:s')." </strong></li>\n";
    $mess .= "    <li style=\"padding-top: 3px;\">Họ tên: <strong> $name</strong></li>\n"; 
    $mess .= "    <li style=\"padding-top: 3px;\">Email : <strong> $email</strong></li>\n";
    $mess .= "    <li style=\"padding-top: 3px;\">Điện thoại: <strong> $phone</strong></li>\n"; 
    $mess .= "    <li style=\"padding-top: 3px;\">Địa chỉ: <strong> $diachi</strong></li>\n";
    $mess .= "    <li style=\"padding-top: 3px;\">Quốc tịch: <strong> $quoctich</strong></li>\n";
    $mess .= "    <li style=\"padding-top: 3px;\">Tên Khách Sạn: <strong> <a href='$link' target='_blank' title='Click để xem chi tiết Khách Sạn'> ".$arr['hos_title']." </a></strong></li>\n";
    $mess .= "    <li style=\"padding-top: 3px;\">Số người lớn: <strong> $nguoilon</strong></li>\n"; 
    $mess .= "    <li style=\"padding-top: 3px;\">Số trẻ em : <strong> $treem</strong></li>\n";
    $mess .= "    <li style=\"padding-top: 3px;\">Ngày muốn đặt:  <strong> $ngaykh</strong></li>\n";   
    $mess .= "    <li style=\"padding-top: 3px;\">Yêu cầu của khách hàng:  <strong> $yeucauthem</strong></li>\n";                           
    $mess .= "</ul>\n"; 
        
    $send = sendlienhe($subject,$mess);
    echo $send;
    
    exit();  
    
  }
?>  